import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-02-07
 * Time: 7:32
 */

class Solution1 {
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;

    public List<List<Integer>> permute(int[] nums) {
        //主函数初始化成员变量
        ret = new ArrayList<>();
        path = new ArrayList<>();
        check = new boolean[nums.length];//必须初始化长度

        dfs(nums);
        return ret;
    }
    public void dfs(int[] nums){
        if(path.size() == nums.length){
            ret.add(new ArrayList<>(path));//相当于copy一份path
            return;
        }
        for(int i = 0 ; i < nums.length ; i++){
            if(check[i] == false){
                path.add(nums[i]);
                check[i] = true;
                //递归
                dfs(nums);//继续枚举
                //枚举完毕，开始回溯
                check[i] = false;
                path.remove(path.size() - 1);//不是i
            }
        }
    }

}
public class Test1 {
}
